package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.Userparam;
import com.example.demo.datasource.multi.dao.organizationmapper;
import com.example.demo.datasource.multi.dao.selectstringmapper;
import com.example.demo.datasource.multi.entity.organization;
import com.example.demo.entity.organizationre;
import com.example.demo.entity.role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


//下拉框选项配置
@Slf4j
@Controller
public class dropdownselectListcontroller {


    @Autowired
    com.example.demo.entity.role role;
    @Autowired
    com.example.demo.datasource.multi.dao.usermapper usermapper;
    @Autowired
    com.example.demo.datasource.multi.dao.organizationmapper organizationmapper;
    @Autowired
    com.example.demo.datasource.multi.dao.selectstringmapper selectstringmapper;

    @ResponseBody
    @RequestMapping("/getdropdownselectList")
    public JSON getAllRoleList(@RequestBody Map<String,String> params, HttpServletRequest request){
        System.out.println(params);

        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        String username= user.getUsername();

        String sql="select * from user where username = '"+username+"'";

        com.example.demo.datasource.multi.entity.user theuser=usermapper.getuser(sql);



        if (params.get("type").equals("roleName")){


            Date date = new Date();

            com.example.demo.entity.role rl = new role();
            rl.setId("1");
            rl.setRoleName("Super Admin");
            rl.setRoleValue("Super");
            rl.setCreateTime(String.valueOf(date));
            rl.setOrderNo("1");
            rl.setStatus(1);

            role rl1 = new role();
            rl1.setId("2");
            rl1.setRoleName("Department");
            rl1.setRoleValue("Department");
            rl1.setCreateTime(String.valueOf(date));
            rl1.setOrderNo("2");
            rl1.setStatus(1);

            role rl2 = new role();
            rl2.setId("3");
            rl2.setRoleName("student");
            rl2.setRoleValue("student");
            rl2.setCreateTime(String.valueOf(date));
            rl2.setOrderNo("3");
            rl2.setStatus(1);

            List<role> rolelist = new ArrayList<>();

            if (theuser.getRolevalue().equals("Super")){
                rolelist.add(rl);
                rolelist.add(rl1);
                rolelist.add(rl2);

            }else {

//                rolelist.add(rl1);
                rolelist.add(rl2);

            }


            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",rolelist);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }else
        if (params.get("type").equals("college")){

            String sql1;
            if (theuser.getRolevalue().equals("Department")){

                sql1 = "SELECT * FROM organization WHERE code = '"+theuser.getCollege()+"' OR  " +
                        "father = '"+theuser.getCollege()+"' " +
                        "OR `father` in (SELECT code FROM organization WHERE type != 'college' AND  father = '"+theuser.getCollege()+"')";
            }else {
                sql1 = "SELECT * FROM organization";

            }

            List<organization> ut = organizationmapper.getorganization(sql1);
            System.out.println(ut);

            int index = 0;
            List<role> collegelist = new ArrayList<>();
            for (organization aa: ut
                 ) {
                if (aa.getType().equals("college")){

                    Date date = new Date();
                    role rl1 = new role();
                    rl1.setId(String.valueOf(++index));
                    rl1.setRoleName(aa.getName());
                    rl1.setRoleValue(aa.getName());
                    rl1.setCreateTime(String.valueOf(date));
                    rl1.setOrderNo(String.valueOf(++index));
                    rl1.setStatus(1);
                    collegelist.add(rl1);
                }
            }

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",collegelist);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }else
        if (params.get("type").equals("profession")){

            String where = "";
            String tcode = "";
            if (!(params.get("father") == null)){
                String father = params.get("father");
                String getcodesql = "select code FROM organization WHERE `name` = '"+father+"'";

                 tcode = selectstringmapper.selectstring(getcodesql);

                where = where + " where T.father = '"+ tcode+"'";
            }

            String sql1;
            if (theuser.getRolevalue().equals("Department")){

                sql1 = "SELECT * FROM (SELECT * FROM organization WHERE code = '"+theuser.getCollege()+"' OR  " +
                        "father = '"+theuser.getCollege()+"' " +
                        "OR `father` in (SELECT code FROM organization WHERE type != 'college' AND  father = '"+theuser.getCollege()+"') ) T " + where;
            }else {
                sql1 = "SELECT T.* FROM organization T " +where;

            }

            List<organization> ut = organizationmapper.getorganization(sql1);
            System.out.println(ut);

            int index = 0;
            List<role> professionlist = new ArrayList<>();
            for (organization aa: ut
            ) {
                if (aa.getType().equals("profession")){

                    Date date = new Date();
                    role rl1 = new role();
                    rl1.setId(String.valueOf(++index));
                    rl1.setRoleName(aa.getName());
                    rl1.setRoleValue(aa.getName());
                    rl1.setCreateTime(String.valueOf(date));
                    rl1.setOrderNo(String.valueOf(++index));
                    rl1.setStatus(1);
                    professionlist.add(rl1);
                }
            }

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",professionlist);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }else
        if (params.get("type").equals("tclass")){
            String where = "";
            String tcode = "";
            if (!(params.get("father") == null)){
                String father = params.get("father");
                String getcodesql = "select code FROM organization WHERE `name` = '"+father+"'";

                tcode = selectstringmapper.selectstring(getcodesql);

                where = where + " where T.father = '"+ tcode+"'";
            }
            String sql1;
            if (theuser.getRolevalue().equals("Department")){

                sql1 = "SELECT * FROM (SELECT * FROM organization WHERE code = '"+theuser.getCollege()+"' OR  " +
                        "father = '"+theuser.getCollege()+"' " +
                        "OR `father` in (SELECT code FROM organization WHERE type != 'college' AND  father = '"+theuser.getCollege()+"') ) T " + where;
            }else {
                sql1 = "SELECT T.* FROM organization T " +where;

            }

            List<organization> ut = organizationmapper.getorganization(sql1);
            System.out.println(ut);

            int index = 0;
            List<role> tclasslist = new ArrayList<>();
            for (organization aa: ut
            ) {
                if (aa.getType().equals("tclass")){

                    Date date = new Date();
                    role rl1 = new role();
                    rl1.setId(String.valueOf(++index));
                    rl1.setRoleName(aa.getName());
                    rl1.setRoleValue(aa.getName());
                    rl1.setCreateTime(String.valueOf(date));
                    rl1.setOrderNo(String.valueOf(++index));
                    rl1.setStatus(1);
                    tclasslist.add(rl1);
                }
            }


            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",tclasslist);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }else
        if (params.get("type").equals("organizationtree")){

            String sql1;
            if (theuser.getRolevalue().equals("Department")){

                sql1 = "SELECT * FROM organization WHERE code = '"+theuser.getCollege()+"' OR  " +
                        "father = '"+theuser.getCollege()+"' " +
                        "OR `father` in (SELECT code FROM organization WHERE type != 'college' AND  father = '"+theuser.getCollege()+"')";
            }else {
                sql1 = "SELECT * FROM organization";

            }

            List<organization> ut = organizationmapper.getorganization(sql1);
            System.out.println(ut);

            List<organizationre> orglist = new ArrayList<>();
            for (organization aa:ut
            ) {
                organizationre college = new organizationre();
                if (aa.getType().equals("college")){
                    college.setCode(aa.getCode());
                    college.setId(aa.getCode());
                    college.setName(aa.getName());
                    college.setType(aa.getType());

                    List<organizationre> professionlist = new ArrayList<>();
                    for (organization bb:ut
                    ) {
                        organizationre profession = new organizationre();
                        if (bb.getType().equals("profession") && aa.getCode().equals(bb.getFather())){
                            profession.setCode(bb.getCode());
                            profession.setId(bb.getCode());
                            profession.setName(bb.getName());
                            profession.setType(bb.getType());
                            profession.setDisabled("true");

                            //再加个for遍历班级
                            List<organizationre> tclasslist = new ArrayList<>();
                            for (organization cc:ut
                            ) {
                                organizationre tclass = new organizationre();
                                if (cc.getType().equals("tclass") && bb.getCode().equals(cc.getFather())){

                                    tclass.setCode(cc.getCode());
                                    tclass.setId( cc.getCode());
                                    tclass.setName(cc.getName());
                                    tclass.setType(cc.getType());

                                    tclass.setChildren(null);

                                    tclasslist.add(tclass);
                                }

                            }
                            profession.setChildren(tclasslist);
                            professionlist.add(profession);
                        }
                    }

                    college.setChildren(professionlist);
                    orglist.add(college);

                }
            }

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",orglist);
            result5.put("message","提交成功!");
            result5.put("type","success");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;


        }
        else {

            Map<String, Object> result5 = new HashMap<String,Object>();

            result5.put("code",-1);
            result5.put("result",null);
            result5.put("message","未配置该类型");
            result5.put("type","error");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }



    }
}
